
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.event.*;
import javax.swing.ListSelectionModel;

public class StudentGUI extends JFrame implements ActionListener{


	private JButton addButton, editButton, deleteButton, uploadButton,saveButton, exitButton, logoutButton;
	private JScrollPane scroll;

	protected static JTable tableStudList;
	protected static StudentTableModel model;
	private ListSelectionModel listSelectionModel;
	private JPanel tablePanel = new JPanel();



	public StudentGUI(){


		addButton = new JButton("Add");
		editButton = new JButton("Edit");
		editButton.setEnabled(false);
		deleteButton = new JButton("Delete");
		deleteButton.setEnabled(false);
		uploadButton = new JButton("Upload a File");
		saveButton = new JButton("Save");
		exitButton = new JButton("Close");
		logoutButton = new JButton("Log Out");


		String[] columnNames = {"ID", "NAME", "COURSE", "YEAR", "CODE", "SCHEDULE"};
		Object[][] data = StudentMainClass.data;

		//Initialize String columnNames and Object data[][] for JTable
		model = new StudentTableModel(data, columnNames);

		tableStudList = new JTable(model);

		//Adds JTable to JScrollPane
		scroll = new JScrollPane(tableStudList);
		tablePanel.add(scroll);


		JPanel buttonPanel = new JPanel(new GridLayout(0,5,10,10));
		buttonPanel.add(addButton);
		buttonPanel.add(editButton);
		buttonPanel.add(deleteButton);
		buttonPanel.add(uploadButton);
		buttonPanel.add(saveButton);
		buttonPanel.add(exitButton);
		buttonPanel.add(logoutButton);

		listSelectionModel = tableStudList.getSelectionModel();
    	listSelectionModel.addListSelectionListener(new SharedListSelectionHandler());

		//addActionListeners
		addButton.addActionListener(this);
		editButton.addActionListener(this);
		deleteButton.addActionListener(this);
		uploadButton.addActionListener(this);
		saveButton.addActionListener(this);
		exitButton.addActionListener(this);
		logoutButton.addActionListener(this);


		//Set the layout of the JFrame with GridBagLayout and GridBagConstraints
		setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();

		c.fill = GridBagConstraints.HORIZONTAL;
		c.weightx = 1;
		c.weighty = 1;
		c.insets = new Insets(2,2,2,2);
		add(tablePanel, c);

		c.weightx = 0;
		c.weighty = 0;
		c.gridy = 2;
		c.insets = new Insets(2,5,5,5);
		add(buttonPanel, c);

		//Sets the variables of the JFrame
		pack();
		setTitle("Student Record Database");
		setVisible(true);
		setResizable(true);
		setLocationRelativeTo(null);
    	setDefaultCloseOperation(EXIT_ON_CLOSE);
	}

	//ActionEvent from the JButtons
	public void actionPerformed(ActionEvent e){
		if(e.getSource() == addButton){
			JDialog jdAdd = new ButtonDialog(this,"ADD");

		}

		else if(e.getSource() == editButton){
			JDialog jdEdit = new ButtonDialog(this,"EDIT");

		}

		else if(e.getSource() == deleteButton){
			int confirm = JOptionPane.showConfirmDialog(null, "Are you sure?", "Confirm",
                        JOptionPane.YES_NO_OPTION);

            if (confirm == 0) {
            	 model.removeRow(tableStudList.getSelectedRow());
            }

		}
		else if(e.getSource() == uploadButton){
				Uploader up = new Uploader();
        		up.setVisible(true);
     			dispose();
                up.setSize(270,180);
                up.setLocation(500,250);
		}

		else if(e.getSource() == saveButton){
			StudentMainClass.saveData();

		}

		else if(e.getSource() == logoutButton){
			Login login = new Login();
			login.setVisible(true);
    		dispose();
			login.setSize(270,180);
			login.setLocation(500,250);

		}


		else if(e.getSource() == exitButton){
			int confirm = JOptionPane.showConfirmDialog(null, "Are you sure?", "Confirm",
                        JOptionPane.YES_NO_OPTION);

            if (confirm == 0) {
            	 System.exit(0);
            }
		}
	}

	//Handles the functionalities of the edit and delete button when a part of the table is selected

	class SharedListSelectionHandler implements ListSelectionListener {
		 public void valueChanged(ListSelectionEvent e) {
		 	editButton.setEnabled(true);
		 	deleteButton.setEnabled(true);
		 }
	}

	class StudentTableModel extends DefaultTableModel{
		public StudentTableModel(Object rowData[][], Object columnNames[]){
        	super(rowData, columnNames);
      	}

		public boolean isCellEditable(int row, int column) {
	    	return false;
	  	}
	}
}
